{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# OpenAI"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can use `Guardrails` with OpenAI's function calling API using a combination of Guardrails and Pydantic. In this notebook, we walk through an example of how to use Guardrails with OpenAI's function calling API for generating structured data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "\n",
    "from pydantic import BaseModel\n",
    "from rich import print\n",
    "from typing import List\n",
    "\n",
    "import guardrails as gd\n",
    "\n",
    "# Set your OPENAI_API_KEY as an environment variable\n",
    "# import os\n",
    "# os.environ['OPENAI_API_KEY'] = \"YOUR_API_KEY\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 1: Create a Pydantic Model representing the data you want to generate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Movie(BaseModel):\n",
    "    rank: int\n",
    "    title: str\n",
    "    details: str\n",
    "\n",
    "\n",
    "class Director(BaseModel):\n",
    "    \"\"\"A movie director\"\"\"\n",
    "\n",
    "    name: str\n",
    "    movies: List[Movie]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 2: Create a `Guard` object with your Pydantic Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "guard = gd.Guard.for_pydantic(Director, prompt=\"Generate data about a movie director.\")\n",
    "\n",
    "raw_llm_output, validated_output, *rest = guard(\n",
    "    openai.chat.completions.create,\n",
    "    model=\"gpt-4-0613\",\n",
    "    max_tokens=1024,\n",
    "    temperature=0.0,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\">{</span>\n",
       "    <span style=\"color: #008000; text-decoration-color: #008000\">'name'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'Christopher Nolan'</span>,\n",
       "    <span style=\"color: #008000; text-decoration-color: #008000\">'movies'</span>: <span style=\"font-weight: bold\">[</span>\n",
       "        <span style=\"font-weight: bold\">{</span>\n",
       "            <span style=\"color: #008000; text-decoration-color: #008000\">'rank'</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>,\n",
       "            <span style=\"color: #008000; text-decoration-color: #008000\">'title'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'Inception'</span>,\n",
       "            <span style=\"color: #008000; text-decoration-color: #008000\">'details'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'A thief who steals corporate secrets through the use of dream-sharing technology is given </span>\n",
       "<span style=\"color: #008000; text-decoration-color: #008000\">the inverse task of planting an idea into the mind of a CEO.'</span>\n",
       "        <span style=\"font-weight: bold\">}</span>,\n",
       "        <span style=\"font-weight: bold\">{</span>\n",
       "            <span style=\"color: #008000; text-decoration-color: #008000\">'rank'</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>,\n",
       "            <span style=\"color: #008000; text-decoration-color: #008000\">'title'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'The Dark Knight'</span>,\n",
       "            <span style=\"color: #008000; text-decoration-color: #008000\">'details'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, Batman </span>\n",
       "<span style=\"color: #008000; text-decoration-color: #008000\">must accept one of the greatest psychological and physical tests of his ability to fight injustice.'</span>\n",
       "        <span style=\"font-weight: bold\">}</span>,\n",
       "        <span style=\"font-weight: bold\">{</span>\n",
       "            <span style=\"color: #008000; text-decoration-color: #008000\">'rank'</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">3</span>,\n",
       "            <span style=\"color: #008000; text-decoration-color: #008000\">'title'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'Interstellar'</span>,\n",
       "            <span style=\"color: #008000; text-decoration-color: #008000\">'details'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">\"A team of explorers travel through a wormhole in space in an attempt to ensure humanity's </span>\n",
       "<span style=\"color: #008000; text-decoration-color: #008000\">survival.\"</span>\n",
       "        <span style=\"font-weight: bold\">}</span>\n",
       "    <span style=\"font-weight: bold\">]</span>\n",
       "<span style=\"font-weight: bold\">}</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1m{\u001b[0m\n",
       "    \u001b[32m'name'\u001b[0m: \u001b[32m'Christopher Nolan'\u001b[0m,\n",
       "    \u001b[32m'movies'\u001b[0m: \u001b[1m[\u001b[0m\n",
       "        \u001b[1m{\u001b[0m\n",
       "            \u001b[32m'rank'\u001b[0m: \u001b[1;36m1\u001b[0m,\n",
       "            \u001b[32m'title'\u001b[0m: \u001b[32m'Inception'\u001b[0m,\n",
       "            \u001b[32m'details'\u001b[0m: \u001b[32m'A thief who steals corporate secrets through the use of dream-sharing technology is given \u001b[0m\n",
       "\u001b[32mthe inverse task of planting an idea into the mind of a CEO.'\u001b[0m\n",
       "        \u001b[1m}\u001b[0m,\n",
       "        \u001b[1m{\u001b[0m\n",
       "            \u001b[32m'rank'\u001b[0m: \u001b[1;36m2\u001b[0m,\n",
       "            \u001b[32m'title'\u001b[0m: \u001b[32m'The Dark Knight'\u001b[0m,\n",
       "            \u001b[32m'details'\u001b[0m: \u001b[32m'When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, Batman \u001b[0m\n",
       "\u001b[32mmust accept one of the greatest psychological and physical tests of his ability to fight injustice.'\u001b[0m\n",
       "        \u001b[1m}\u001b[0m,\n",
       "        \u001b[1m{\u001b[0m\n",
       "            \u001b[32m'rank'\u001b[0m: \u001b[1;36m3\u001b[0m,\n",
       "            \u001b[32m'title'\u001b[0m: \u001b[32m'Interstellar'\u001b[0m,\n",
       "            \u001b[32m'details'\u001b[0m: \u001b[32m\"A team of explorers travel through a wormhole in space in an attempt to ensure humanity's \u001b[0m\n",
       "\u001b[32msurvival.\"\u001b[0m\n",
       "        \u001b[1m}\u001b[0m\n",
       "    \u001b[1m]\u001b[0m\n",
       "\u001b[1m}\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(validated_output)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (Optional) Step 3: View logs of the prompt and response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Logs\n",
       "└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮\n",
       "    │ <span style=\"background-color: #f0f8ff\">╭──────────────────────────────────────────────── Prompt ─────────────────────────────────────────────────╮</span> │\n",
       "    │ <span style=\"background-color: #f0f8ff\">│ Generate data about a movie director.                                                                   │</span> │\n",
       "    │ <span style=\"background-color: #f0f8ff\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">╭──────────────────────────────────────────── Raw LLM Output ─────────────────────────────────────────────╮</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│ {                                                                                                       │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│   \"director\": {                                                                                         │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│     \"name\": \"Christopher Nolan\",                                                                        │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│     \"birth_date\": \"1970-07-30\",                                                                         │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│     \"nationality\": \"British\",                                                                           │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│     \"awards\": [                                                                                         │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│       {                                                                                                 │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│         \"name\": \"Academy Award for Best Director\",                                                      │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│         \"year\": 2010                                                                                    │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│       },                                                                                                │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│       {                                                                                                 │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│         \"name\": \"Golden Globe Award for Best Director\",                                                 │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│         \"year\": 2011                                                                                    │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│       },                                                                                                │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│       {                                                                                                 │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│         \"name\": \"BAFTA Award for Best Direction\",                                                       │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│         \"year\": 2011                                                                                    │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│       }                                                                                                 │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│     ],                                                                                                  │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│     \"movies\": [                                                                                         │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│       {                                                                                                 │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│         \"title\": \"Memento\",                                                                             │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│         \"year\": 2000,                                                                                   │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│         \"genre\": \"Mystery/Thriller\"                                                                     │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│       },                                                                                                │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│       {                                                                                                 │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│         \"title\": \"The Dark Knight\",                                                                     │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│         \"year\": 2008,                                                                                   │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│         \"genre\": \"Action/Crime\"                                                                         │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│       },                                                                                                │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│       {                                                                                                 │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│         \"title\": \"Inception\",                                                                           │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│         \"year\": 2010,                                                                                   │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│         \"genre\": \"Science Fiction/Action\"                                                               │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│       }                                                                                                 │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│     ]                                                                                                   │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│   }                                                                                                     │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│ }                                                                                                       │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">╭─────────────────────────────────────────── Validated Output ────────────────────────────────────────────╮</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│ {                                                                                                       │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│     'director': {                                                                                       │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│         'name': 'Christopher Nolan',                                                                    │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│         'birth_date': '1970-07-30',                                                                     │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│         'nationality': 'British',                                                                       │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│         'awards': [                                                                                     │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│             {'name': 'Academy Award for Best Director', 'year': 2010},                                  │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│             {'name': 'Golden Globe Award for Best Director', 'year': 2011},                             │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│             {'name': 'BAFTA Award for Best Direction', 'year': 2011}                                    │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│         ],                                                                                              │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│         'movies': [                                                                                     │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│             {'title': 'Memento', 'year': 2000, 'genre': 'Mystery/Thriller'},                            │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│             {                                                                                           │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│                 'title': 'The Dark Knight',                                                             │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│                 'year': 2008,                                                                           │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│                 'genre': 'Action/Crime'                                                                 │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│             },                                                                                          │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│             {                                                                                           │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│                 'title': 'Inception',                                                                   │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│                 'year': 2010,                                                                           │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│                 'genre': 'Science Fiction/Action'                                                       │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│             }                                                                                           │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│         ]                                                                                               │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│     }                                                                                                   │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│ }                                                                                                       │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
       "    ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Logs\n",
       "└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮\n",
       "    │ \u001b[48;2;240;248;255m╭─\u001b[0m\u001b[48;2;240;248;255m───────────────────────────────────────────────\u001b[0m\u001b[48;2;240;248;255m Prompt \u001b[0m\u001b[48;2;240;248;255m────────────────────────────────────────────────\u001b[0m\u001b[48;2;240;248;255m─╮\u001b[0m │\n",
       "    │ \u001b[48;2;240;248;255m│\u001b[0m\u001b[48;2;240;248;255m \u001b[0m\u001b[48;2;240;248;255mGenerate data about a movie director.\u001b[0m\u001b[48;2;240;248;255m                                                                  \u001b[0m\u001b[48;2;240;248;255m \u001b[0m\u001b[48;2;240;248;255m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;248;255m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m╭─\u001b[0m\u001b[48;2;245;245;220m───────────────────────────────────────────\u001b[0m\u001b[48;2;245;245;220m Raw LLM Output \u001b[0m\u001b[48;2;245;245;220m────────────────────────────────────────────\u001b[0m\u001b[48;2;245;245;220m─╮\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m{\u001b[0m\u001b[48;2;245;245;220m                                                                                                      \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m  \"director\": {\u001b[0m\u001b[48;2;245;245;220m                                                                                        \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m    \"name\": \"Christopher Nolan\",\u001b[0m\u001b[48;2;245;245;220m                                                                       \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m    \"birth_date\": \"1970-07-30\",\u001b[0m\u001b[48;2;245;245;220m                                                                        \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m    \"nationality\": \"British\",\u001b[0m\u001b[48;2;245;245;220m                                                                          \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m    \"awards\": [\u001b[0m\u001b[48;2;245;245;220m                                                                                        \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m      {\u001b[0m\u001b[48;2;245;245;220m                                                                                                \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m        \"name\": \"Academy Award for Best Director\",\u001b[0m\u001b[48;2;245;245;220m                                                     \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m        \"year\": 2010\u001b[0m\u001b[48;2;245;245;220m                                                                                   \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m      },\u001b[0m\u001b[48;2;245;245;220m                                                                                               \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m      {\u001b[0m\u001b[48;2;245;245;220m                                                                                                \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m        \"name\": \"Golden Globe Award for Best Director\",\u001b[0m\u001b[48;2;245;245;220m                                                \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m        \"year\": 2011\u001b[0m\u001b[48;2;245;245;220m                                                                                   \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m      },\u001b[0m\u001b[48;2;245;245;220m                                                                                               \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m      {\u001b[0m\u001b[48;2;245;245;220m                                                                                                \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m        \"name\": \"BAFTA Award for Best Direction\",\u001b[0m\u001b[48;2;245;245;220m                                                      \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m        \"year\": 2011\u001b[0m\u001b[48;2;245;245;220m                                                                                   \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m      }\u001b[0m\u001b[48;2;245;245;220m                                                                                                \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m    ],\u001b[0m\u001b[48;2;245;245;220m                                                                                                 \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m    \"movies\": [\u001b[0m\u001b[48;2;245;245;220m                                                                                        \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m      {\u001b[0m\u001b[48;2;245;245;220m                                                                                                \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m        \"title\": \"Memento\",\u001b[0m\u001b[48;2;245;245;220m                                                                            \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m        \"year\": 2000,\u001b[0m\u001b[48;2;245;245;220m                                                                                  \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m        \"genre\": \"Mystery/Thriller\"\u001b[0m\u001b[48;2;245;245;220m                                                                    \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m      },\u001b[0m\u001b[48;2;245;245;220m                                                                                               \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m      {\u001b[0m\u001b[48;2;245;245;220m                                                                                                \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m        \"title\": \"The Dark Knight\",\u001b[0m\u001b[48;2;245;245;220m                                                                    \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m        \"year\": 2008,\u001b[0m\u001b[48;2;245;245;220m                                                                                  \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m        \"genre\": \"Action/Crime\"\u001b[0m\u001b[48;2;245;245;220m                                                                        \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m      },\u001b[0m\u001b[48;2;245;245;220m                                                                                               \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m      {\u001b[0m\u001b[48;2;245;245;220m                                                                                                \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m        \"title\": \"Inception\",\u001b[0m\u001b[48;2;245;245;220m                                                                          \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m        \"year\": 2010,\u001b[0m\u001b[48;2;245;245;220m                                                                                  \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m        \"genre\": \"Science Fiction/Action\"\u001b[0m\u001b[48;2;245;245;220m                                                              \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m      }\u001b[0m\u001b[48;2;245;245;220m                                                                                                \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m    ]\u001b[0m\u001b[48;2;245;245;220m                                                                                                  \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m  }\u001b[0m\u001b[48;2;245;245;220m                                                                                                    \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m}\u001b[0m\u001b[48;2;245;245;220m                                                                                                      \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m╭─\u001b[0m\u001b[48;2;240;255;240m──────────────────────────────────────────\u001b[0m\u001b[48;2;240;255;240m Validated Output \u001b[0m\u001b[48;2;240;255;240m───────────────────────────────────────────\u001b[0m\u001b[48;2;240;255;240m─╮\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m{\u001b[0m\u001b[48;2;240;255;240m                                                                                                      \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m    'director': {\u001b[0m\u001b[48;2;240;255;240m                                                                                      \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m        'name': 'Christopher Nolan',\u001b[0m\u001b[48;2;240;255;240m                                                                   \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m        'birth_date': '1970-07-30',\u001b[0m\u001b[48;2;240;255;240m                                                                    \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m        'nationality': 'British',\u001b[0m\u001b[48;2;240;255;240m                                                                      \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m        'awards': [\u001b[0m\u001b[48;2;240;255;240m                                                                                    \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m            {'name': 'Academy Award for Best Director', 'year': 2010},\u001b[0m\u001b[48;2;240;255;240m                                 \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m            {'name': 'Golden Globe Award for Best Director', 'year': 2011},\u001b[0m\u001b[48;2;240;255;240m                            \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m            {'name': 'BAFTA Award for Best Direction', 'year': 2011}\u001b[0m\u001b[48;2;240;255;240m                                   \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m        ],\u001b[0m\u001b[48;2;240;255;240m                                                                                             \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m        'movies': [\u001b[0m\u001b[48;2;240;255;240m                                                                                    \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m            {'title': 'Memento', 'year': 2000, 'genre': 'Mystery/Thriller'},\u001b[0m\u001b[48;2;240;255;240m                           \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m            {\u001b[0m\u001b[48;2;240;255;240m                                                                                          \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m                'title': 'The Dark Knight',\u001b[0m\u001b[48;2;240;255;240m                                                            \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m                'year': 2008,\u001b[0m\u001b[48;2;240;255;240m                                                                          \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m                'genre': 'Action/Crime'\u001b[0m\u001b[48;2;240;255;240m                                                                \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m            },\u001b[0m\u001b[48;2;240;255;240m                                                                                         \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m            {\u001b[0m\u001b[48;2;240;255;240m                                                                                          \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m                'title': 'Inception',\u001b[0m\u001b[48;2;240;255;240m                                                                  \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m                'year': 2010,\u001b[0m\u001b[48;2;240;255;240m                                                                          \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m                'genre': 'Science Fiction/Action'\u001b[0m\u001b[48;2;240;255;240m                                                      \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m            }\u001b[0m\u001b[48;2;240;255;240m                                                                                          \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m        ]\u001b[0m\u001b[48;2;240;255;240m                                                                                              \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m    }\u001b[0m\u001b[48;2;240;255;240m                                                                                                  \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m}\u001b[0m\u001b[48;2;240;255;240m                                                                                                      \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n",
       "    ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "guard.history.last.tree"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "gd-base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
